var numberKeyboard = null;
window.onload=initInput;
setUpEvent(document, "touchstart", function(e) {
    var elem = e.srcElement || e.target;
    var noNeedHideIds = ["NumberKeyboard", "password"];
    while(elem) {
        if(noNeedHideIds.indexOf(elem.id) !== -1) {
            return;
        }
        elem = elem.parentNode;
    }
    numberKeyboard.hideKeyboard();
});

function doneCallback(sipboxId) {
	var length = $("input[name='password']").val().length;
	if(length != 6){
		BCSLayer.showMsg("密码长度不够6位")
		return false;
	}
	numberKeyboard.hideKeyboard();
	getEncrypt(sipboxId);
}
function initInput() {
    initNumberKeyboard();
    initSipBoxNum("password");
}
function setUpEvent(elem, eventType, handler) {
    return (elem.attachEvent ? elem.attachEvent("on" + eventType, handler)
            : ((elem.addEventListener) ? elem.addEventListener(eventType, handler, false) : null));
}
function initNumberKeyboard() {
    if(numberKeyboard == null) {
        numberKeyboard = new CFCAKeyboard("NumberKeyboard", KEYBOARD_TYPE_DIGITAL);
    }
    var x = document.getElementById("password");
    numberKeyboard.setDoneCallback(doneCallback);
    numberKeyboard.bindInputBox(x || "password");
    numberKeyboard.hideKeyboard();
    var serverRandom = $("input[name='serverRandom']").val();
    if(CFCA_OK != numberKeyboard.setServerRandom(serverRandom)) BCSLayer.showMsg("setServerRandom error");
}
function initSipBoxNum(sipboxId) {
	setProperty(sipboxId);
    var sipBox = document.getElementById(sipboxId);
    setUpEvent(sipBox, "focus",function(event) {
        sipBox.blur();
        numberKeyboard.bindInputBox(sipboxId);
        numberKeyboard.randomNumber(true);
        numberKeyboard.showKeyboard();
    });
}
function getKeyboard(sipboxId) {
    return numberKeyboard;
}
function setProperty(sipboxId) {
    var keyboard = getKeyboard(sipboxId);
    var outputType = OUTPUT_TYPE_ORIGINAL;
    var cipherType = CIPHER_TYPE_RSA;
    var minLength = 6;
    var maxLength = 6;
    var matchRegex = "";
    var serverRandom = $("input[name='serverRandom']").val();
    if(CFCA_OK != keyboard.setMinLength(minLength, sipboxId)) BCSLayer.showMsg("setMinLength error");
    if(CFCA_OK != keyboard.setMaxLength(maxLength, sipboxId)) BCSLayer.showMsg("setMaxLength error");
    if(CFCA_OK != keyboard.setOutputType(outputType, sipboxId)) BCSLayer.showMsg("setOutputType error");
    if(CFCA_OK != keyboard.setServerRandom(serverRandom, sipboxId)) BCSLayer.showMsg("setServerRandom error");
    if(CFCA_OK != keyboard.setCipherType(cipherType, sipboxId)) BCSLayer.showMsg("setCipherType error");
    if(CFCA_OK != keyboard.setMatchRegex(matchRegex, sipboxId)) BCSLayer.showMsg("setMatchRegex error");
}
function getEncrypt(sipboxId) {
    var keyboard = getKeyboard(sipboxId);
    
    var pwdEncryptedData = document.getElementById("pwdEncryptedData");
    var encryptedInputValue = keyboard.getEncryptedInputValue(sipboxId);
    var errorCode = keyboard.getErrorCode(sipboxId).toString(16);
    if(errorCode != CFCA_OK) {
        pwdEncryptedData.value = "加密输入数据错误: 0x" + errorCode + "\n";
        return;
    } else{
        pwdEncryptedData.value = encryptedInputValue;
    }
    
    var rcEncryptedData = document.getElementById("rcEncryptedData");
    var encryptedClientRandom = keyboard.getEncryptedClientRandom(sipboxId);
    errorCode = keyboard.getErrorCode(sipboxId).toString(16);
    if(errorCode != CFCA_OK) {
        rcEncryptedData.value = "加密客户端随机数错误: 0x" + errorCode + "\n";
        return;
    } else{
        rcEncryptedData.value = encryptedClientRandom;
    }
}

function clearInput(sipboxId) {
    var keyboard = getKeyboard(sipboxId);
    keyboard.clearInputValue(sipboxId);
}
